package com.android.launcher3;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuItemView extends LinearLayout {
	private ColorFilter mColorFilter;
	private Drawable mBadgeDrawable;
	private Drawable mFocusedDrawable;
	private Paint mPaint;
	private Rect mTmpRect;
	private boolean mIsFocused;
	private boolean mIsPressed;
	private int mBadgeRightOffset;
	private int mBadgeTopOffset;
	private int mFocusMargin;
	private int mFontSizeDefault;
	private int mFontSizeSmall;
	private int mTopOffset;
	private static final String TAG = "MenuItemView";

	/**
	 * Creates a new MenuItemView object.
	 */
	public MenuItemView(final Context context) {
		this(context, null);
	}

	/**
	 * Creates a new MenuItemView object.
	 */
	public MenuItemView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		this.mTmpRect = new Rect();
		this.mPaint = new Paint();
		init();
	}

	public void setBackgroundDrawable(final Drawable d) {
		((ImageView) findViewById(R.id.icon)).setBackgroundDrawable(d);
	}

	@Override
	public void setBackgroundResource(final int resid) {
		((ImageView) findViewById(R.id.icon)).setBackgroundResource(resid);
	}

	public void setImageDrawable(final Drawable drawable) {
		((ImageView) findViewById(R.id.icon)).setImageDrawable(drawable);
	}

	public void setText(final CharSequence text) {
		((TextView) findViewById(R.id.name)).setText(text);
	}

	protected void dispatchDraw(final Canvas canvas) {
		LogUtils.d(TAG, "dispatchDraw-start");
		super.dispatchDraw(canvas);

		ViewParent localViewParent = getParent();

		if ((localViewParent instanceof UninstallableMarkerDrawParent)) {
			((UninstallableMarkerDrawParent) localViewParent)
					.drawChildUninstallableMarker(canvas, this);
		}
		LogUtils.d(TAG, "dispatchDraw-end");
	}

	protected boolean drawChild(final Canvas canvas, final View view,
			final long drawingTime) {
		LogUtils.d(TAG, "drawChild");
		boolean bSelect = (view instanceof ImageView);

		// Log.d(TAG, "drawChild:bSelect:" + bSelect);
		if (bSelect) {
			if (this.mIsPressed) {
				// Log.e(TAG, "drawChild  Pressed:");
				ImageView localImageView = (ImageView) view;
				Drawable localDrawable1 = localImageView.getDrawable();

				if (localDrawable1 != null) {
					localDrawable1.setColorFilter(this.mColorFilter);
				}

				Drawable localDrawable2 = localImageView.getBackground();

				if (localDrawable2 != null) {
					localDrawable2.setColorFilter(this.mColorFilter);
				}

				boolean bool1 = super.drawChild(canvas, view, drawingTime);

				if (localDrawable1 != null) {
					localDrawable1.clearColorFilter();
				}

				if (localDrawable2 != null) {
					localDrawable2.clearColorFilter();
				}

				return bool1;
			} else {
				if (this.mIsFocused) {
					int i = this.mFocusMargin;
					Rect localRect = this.mTmpRect;
					int j = view.getLeft() - i;
					localRect.left = j;

					int k = view.getLeft();
					int m = view.getWidth();
					int n = k + m + i;
					localRect.right = n;

					int i1 = view.getTop() - i;
					localRect.top = i1;

					int i2 = view.getTop();
					int i3 = view.getHeight();
					int i4 = i2 + i3 + i;
					localRect.bottom = i4;
					this.mFocusedDrawable.setBounds(localRect);
					this.mFocusedDrawable.draw(canvas);

					return super.drawChild(canvas, view, drawingTime);
				}
			}
		}

		return super.drawChild(canvas, view, drawingTime);
	}

	@Override
	protected void drawableStateChanged() {
		// Log.e(TAG, "drawableStateChanged");
		super.drawableStateChanged();

		int[] arrayOfInt = getDrawableState();
		Drawable localDrawable = ((ImageView) findViewById(R.id.icon))
				.getBackground();

		if (localDrawable != null) {
			localDrawable.setState(arrayOfInt);
		}

		int iCount = arrayOfInt.length;
		int j;

		for (j = 0; j < iCount; j++) {
			// Log.e(TAG, "drawableStateChanged   state  " + arrayOfInt[j]);
			if (arrayOfInt[j] == android.R.attr.state_pressed) {
				// Log.e(TAG, "drawableStateChanged   state_pressed");
				this.mIsPressed = true;

				invalidate();

				return;
			}

			if (arrayOfInt[j] == android.R.attr.state_focused) {
				// Log.e(TAG, "drawableStateChanged   state_focused");
				this.mIsFocused = true;
				this.mIsPressed = false;
				invalidate();

				continue;
			}
		}

		if ((!this.mIsPressed) && (!this.mIsFocused)) {
			return;
		}

		this.mIsPressed = false;
		this.mIsFocused = false;
		invalidate();
	}

	@Override
	protected void onLayout(final boolean changed, final int l, final int t,
			final int r, final int b) {
		LogUtils.d(TAG, "onLayout");
		super.onLayout(changed, l, t, r, b);

		int i = getChildCount();
		int j = this.mTopOffset;
		// android.util.Log.e(TAG, "onLayout getChildCount--" + i);
		if (j > 0) {
			int k = 0;

			while (k < i) {
				View localView = getChildAt(k);
				int m = localView.getLeft();
				int n = localView.getTop() + j;
				int i1 = localView.getRight();
				int i2 = localView.getBottom() + j;
				localView.layout(m, n, i1, i2);
				k++;
			}
		}
	}

	private void init() {
		Resources res = getResources();
		this.mTopOffset = res
				.getDimensionPixelSize(R.dimen.menu_item_top_space);

		this.mFocusMargin = res
				.getDimensionPixelSize(R.dimen.menu_item_focus_margin);

		this.mFocusedDrawable = res
				.getDrawable(R.drawable.focused_application_background);

		this.mBadgeDrawable = res
				.getDrawable(R.drawable.homescreen_menu_noti_bg);

		this.mColorFilter = new PorterDuffColorFilter(2130706432,
				PorterDuff.Mode.SRC_ATOP);
		;

		this.mBadgeTopOffset = res
				.getDimensionPixelSize(R.dimen.badge_top_offset);

		this.mBadgeRightOffset = res
				.getDimensionPixelSize(R.dimen.badge_right_offset);

		this.mFontSizeDefault = res
				.getDimensionPixelSize(R.dimen.badge_fontsize_default);

		this.mFontSizeSmall = res
				.getDimensionPixelSize(R.dimen.badge_fontsize_small);
		this.mPaint.setAntiAlias(true);

		this.mPaint.setTextSize(this.mFontSizeDefault);
		this.mPaint.setColor(-1);
		this.mPaint.setTextAlign(Paint.Align.CENTER);
	}

	public abstract interface UninstallableMarkerDrawParent {
		public abstract void drawChildUninstallableMarker(
				final Canvas paramCanvas, final View paramView);
	}
}
